package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.dao.EmployeeDao;
import cn.usian.entity.Employee;
import cn.usian.exception.NameException;
import cn.usian.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeDao employeeDao;
    @Override
    public R<Employee> login(Employee employee) {
        //1.检查用户输入的用户名是否存在==>根据用户名来查询用户信息
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        //添加用户名的查询条件
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        //执行查询
        Employee selectOne = employeeDao.selectOne(queryWrapper);
        //如果用户名不存在直接返回错误信息
        if(selectOne==null){
          return   R.error("用户不存在");
        }
        //2.判定密码是否存在 md5
        //获取用户输入的密码进行md5的转换
        String hex = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if(!hex.equals(selectOne.getPassword())){
            return R.error("密码不正确");
        }
        //3.判断状态
        if(selectOne.getStatus()==0){
            return R.error("账号已被禁用");
        }
        return R.success(selectOne);
    }

    @Override
    public void save(Employee employee) {
        //判断当前用户名是否存在
        //1.检查用户输入的用户名是否存在==>根据用户名来查询用户信息
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        //添加用户名的查询条件
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        //执行查询
        Employee selectOne = employeeDao.selectOne(queryWrapper);
        if(selectOne!=null){
            throw new NameException(employee.getUsername());
        }
        //保存用户得创建时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()).toString());
        employeeDao.insert(employee);
    }

    @Override
    public R findPage(int page, int pageSize, String name) {
         LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
         queryWrapper.like(name!=null, Employee::getName,name);
        //创建Page对象
        Page<Employee> pageInfo=new Page<>(page,pageSize);
        //调用selectpage方法来进行分页
        //自定义过滤器
        //debug调试
        Page<Employee> selectPage = employeeDao.selectPage(pageInfo, queryWrapper);
        return R.success(selectPage);
    }

    @Override
    public void updateEmp(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<Employee> updateWrapper=new LambdaUpdateWrapper<>();
        //判断用户名 name
        if(employee.getName()!=null&&!employee.equals("")){
            updateWrapper.set(Employee::getName,employee.getName());
        }
        //判定登录名 username
        if(employee.getUsername()!=null&&!employee.equals("")){
            updateWrapper.set(Employee::getUsername,employee.getUsername());
        }
        //判定手机号 phone
        if(employee.getPhone()!=null&&!employee.equals("")){
            updateWrapper.set(Employee::getPhone,employee.getPhone());
        }
        //判定性别 sex
        if(employee.getSex()!=null&&!employee.equals("")){
            updateWrapper.set(Employee::getSex,employee.getSex());
        }
        //判定身份证号码 idNumber
        if(employee.getIdNumber()!=null&&!employee.equals("")){
            updateWrapper.set(Employee::getIdNumber,employee.getIdNumber());
        }
        //判定状态 status
        if(employee.getStatus()!=null){
            updateWrapper.set(Employee::getStatus,employee.getStatus());
        }
        updateWrapper.eq(Employee::getId,employee.getId());
        employeeDao.update(employee,updateWrapper);
    }

    @Override
    public R findById(Long id) {
        return R.success(employeeDao.selectById(id));
    }
}
